package com.android.baseconfig.common.helper;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.nfc.NfcAdapter;

import com.android.baseconfig.common.utils.Logc;
import com.android.baseconfig.common.utils.NetWorkUtil;

import java.util.Objects;


/**
 * ---------------------------------------------------------------- <br>
 * Copyright (C) 2014-2020, by het, Shenzhen, All rights reserved.  <br>
 * ---------------------------------------------------------------- <br>
 * <p>
 * 描述: NetConnectionReceiver <br>
 * 作者: lei <br>
 * 日期: 2017/7/25 <br>
 */
public class NetConnectionReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Logc.i("llll", "network state change: " + intent.getAction());
        if (Objects.equals(intent.getAction(), ConnectivityManager.CONNECTIVITY_ACTION)) {
            int connectionType = NetWorkUtil.getConnectionType(context);
            Logc.i("llll", "network connectionType: " + connectionType);
            // 更改网络状态
            if (NetObserverManager.getInstance() != null) {
                NetObserverManager.getInstance().notifyNetObserver(connectionType);
            }
        } else if (Objects.equals(intent.getAction(), NfcAdapter.ACTION_ADAPTER_STATE_CHANGED)) {
            NfcAdapter adapter = NfcAdapter.getDefaultAdapter(context);
            Logc.i("llll", "nfc state changed: " + adapter.isEnabled());
            // 更改NFC状态
            if (NetObserverManager.getInstance() != null) {
                NetObserverManager.getInstance().notifyNFCObserver(adapter.isEnabled() ? 0 : 2);
            }
        } else if (Objects.equals(intent.getAction(), WifiManager.WIFI_STATE_CHANGED_ACTION)) {
            int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
            // 更改wifi状态：打开或者关闭
            if (NetObserverManager.getInstance() != null) {
                NetObserverManager.getInstance().notifyWifiStatusObserver(wifiState);
            }
        }
    }
}
